﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Swan.WakeUpLibrary.Library
{
    public class MP3Player
    {
        
        [DllImport("winmm.dll")]
        public static extern long waveOutSetVolume(UInt32 deviceID, UInt32 Volume);
        [DllImport("winmm.dll")]
        public static extern long waveOutGetVolume(UInt32 deviceID, out UInt32 Volume);
        
        private string PlayCommand;
        private bool isOpen;

        [DllImport("winmm.dll")]
        private static extern long mciSendString(
        string strCommand,
        StringBuilder strReturn,
        int iReturnLength,
        IntPtr oCallback);

        public void OpenMediaFile(string strFileName)
        {
            PlayCommand = "Open \"" + strFileName + "\" alias MediaFile";
            mciSendString(PlayCommand, null, 0, IntPtr.Zero);
            isOpen = true;
        }

        public void ClosePlayer()
        {
            PlayCommand = "Close MediaFile";
            mciSendString(PlayCommand, null, 0, IntPtr.Zero);
            isOpen = false;
        }

        public void PlayMediaFile(bool loop)
        {
            if (isOpen)
            {
                PlayCommand = "Play MediaFile";
                if (loop)
                    PlayCommand += " REPEAT";
                mciSendString(PlayCommand, null, 0, IntPtr.Zero);
            }
        }

        /// <summary>
        /// Get/Set Main Volume Factor
        /// </summary>
        public int MasterVolume
        {
            get
            {
                return 0; //Guess could be used to return Volume level: I don't need it
            }
            set
            {
                PlayCommand = string.Concat("setaudio MediaFile volume to ", value);
                mciSendString(PlayCommand, null, 0, IntPtr.Zero);
            }
        }

    }
}
